<template>
  <div class="app-container">
    <div class="top_filter">
      <div class="top_filter_col">
        <span class="top_filter_col_tit">交易日期</span>
        <el-date-picker clearable
                        v-model="queryParams.createtime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择交易日期">
        </el-date-picker>
      </div>
      <div class="top_filter_col">
        <span class="top_filter_col_tit">设备名称</span>
        <el-input
          v-model="queryParams.dname"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </div>

      <div class="top_filter_col">
        <span class="top_filter_col_tit">设备总数</span>
        <el-input
          v-model="queryParams.dcount"
          placeholder="请输入设备总数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </div>

      <div class="top_filter_col">
				<span class="top_filter_col_tit">
					<el-button class="el-icon-search" size="small" @click="handleQuery()" type="success">搜索</el-button>
					<el-button class="el-icon-search" size="small" type="primary" @click="resetQuery">重置</el-button>
				</span>
      </div>

    </div>
    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange" border>
      <el-table-column label="设备编号" align="center" prop="did" />
      <el-table-column label="设备名称" align="center" prop="dname" />
      <el-table-column label="设备编号" align="center" prop="dcode" />
      <el-table-column label="设备金额" align="center" prop="money" />
      <el-table-column label="预计使用月数" align="center" prop="canUse" />
      <el-table-column label="开始折旧时间" align="center" prop="startdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备总数" align="center" prop="dcount" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已经折旧的月数" align="center" prop="deprecatedMonth" />
      <el-table-column label="是否已经完成折旧" align="center" prop="finished" />
      <el-table-column label="所属账户" align="center" prop="ofAccount">
        <template slot-scope="i" >
          <span v-for="it in accountList" v-if="i.row.ofAccount==it.aid">
            {{it.aname}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="币种" align="center" prop="ofCurrency">
        <template slot-scope="i">
          <span  v-for="it in currencyList" v-if="i.row.ofCurrency==it.cid">
            {{it.cnName}}({{it.enName}})
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备折旧对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="dname">
          <el-input v-model="form.dname" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="dcode">
          <el-input v-model="form.dcode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入设备金额" />
        </el-form-item>
        <el-form-item label="预计使用月数" prop="canUse">
          <el-input v-model="form.canUse" placeholder="请输入预计使用月数" />
        </el-form-item>
        <el-form-item label="开始折旧时间" prop="startdate">
          <el-date-picker clearable
                          v-model="form.startdate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始折旧时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备总数" prop="dcount">
          <el-input v-model="form.dcount" placeholder="请输入设备总数" />
        </el-form-item>
        <el-form-item label="已经折旧的月数" prop="deprecatedMonth">
          <el-input v-model="form.deprecatedMonth" placeholder="请输入已经折旧的月数" />
        </el-form-item>
        <el-form-item label="是否已经完成折旧" prop="finished">
          <el-input v-model="form.finished" placeholder="请输入是否已经完成折旧" />
        </el-form-item>
        <el-form-item label="${comment}" prop="ofAccount">
          <el-input v-model="form.ofAccount" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="ofCurrency">
          <el-input v-model="form.ofCurrency" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/system/device";
  import { getInfo } from "@/api/login";
  import {listAccount, income} from '@/api/system/account'
  import {listCurrency} from '@/api/system/currency';
  import {listBalance, addBalance, updateBalance, getBalance} from '@/api/system/balance';
  import {listReason} from '@/api/system/reason';
  export default {
    name: "Device",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备折旧表格数据
        deviceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dname: null,
          dcode: null,
          money: null,
          canUse: null,
          startdate: null,
          dcount: null,
          createtime: null,
          deprecatedMonth: null,
          finished: null,
          ofAccount: null,
          ofCurrency: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        accountList:null,
        currencyList:null,
        reasonList:null,

      };
    },
    created() {
      this.getList();

    },
    methods: {
      /** 查询设备折旧列表 */
      getList() {
        this.loading = true;
        getInfo().then(data=>{
          this.queryParams.ofCompany=data.tuser.ofCompany;
          listDevice(this.queryParams).then(response => {
            this.deviceList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
          listAccount({ofCompany:data.tuser.ofCompany}).then(date => {
            this.accountList = date.rows;
          })
          listCurrency().then(date => {
            this.currencyList = date.rows;
          })
          listReason().then(date => {
            this.reasonList = date.rows;
          })
        })

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          did: null,
          dname: null,
          dcode: null,
          money: null,
          canUse: null,
          startdate: null,
          dcount: null,
          createtime: null,
          deprecatedMonth: null,
          finished: null,
          ofAccount: null,
          ofCurrency: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.did)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加设备折旧";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const did = row.did || this.ids
        getDevice(did).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改设备折旧";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.did != null) {
              updateDevice(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDevice(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dids = row.did || this.ids;
        this.$modal.confirm('是否确认删除设备折旧编号为"' + dids + '"的数据项？').then(function() {
          return delDevice(dids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/device/export', {
          ...this.queryParams
        }, `device_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style lang="scss">
  .pagination_box {
    height: 60px;
    width: 100%;
    background-color: #fff;
  }

  .btn {
    padding: 4px 8px;
    color: white;
    border-radius: 4px;
  }

  .top_filter {
    font-size: 15px;
    padding: 10px 20px;
    background-color: #fff;
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;

    &_col {
      padding: 4px 0;
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-right: 10px;

      &_tit {
        white-space: nowrap;
        margin: 8px;
      }
    }
  }
</style>
